package cn.foo.controller;

import cn.foo.pojo.User;
import cn.foo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * @author Frank
 * @date 07-06
 */
@Controller
public class LoginController {
    @RequestMapping("/login")
    public String login(){
        return "login";
    }
    @Autowired
    private UserService userService;
    @RequestMapping("/doLogin")
    public String doLogin(@RequestParam("userCode")String userCode,
                          @RequestParam("userPassword")String userPassword,
                          HttpServletRequest request, HttpSession session){
        User user = userService.login(userCode, userPassword);
        if(user != null){
            session.setAttribute("userSession",user);
            return "redirect:/main";
        }else{
            request.setAttribute("error","用户名或者密码有误！！");
            return "login";
        }
    }

    @RequestMapping("/main")
    public String main(){
        return "frame";
    }
}
